/*
 * 文 件 名:  UserServiceProviderImpl.java
 * 创建日期:  2012-9-17
 * 版    权:  Epro Technologies Co., Ltd. Copyright 2011-2012,  All rights reserved
 * 作    者:  hjd008
 */
package com.weipu.common.facade.impl;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.weipu.common.DXApplication;
import com.weipu.common.constants.Constant;
import com.weipu.common.constants.Constant.HttpMethod;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.UserServiceProvider;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.model.AddressModel;
import com.weipu.common.facade.model.CountryModel;
import com.weipu.common.facade.model.Messages;
import com.weipu.common.facade.model.OneKeyFilterResponseModel;
import com.weipu.common.facade.model.Parmars;
import com.weipu.common.facade.model.ReviewlistModel;
import com.weipu.common.facade.model.UserModel;
import com.weipu.common.facade.model.WishModel;
import com.weipu.common.facade.model.config.CacheConfigModel;
import com.weipu.common.okhttp.OkHttpHelper;
import com.weipu.common.util.GlobalConfig;
import com.weipu.common.util.GlobalConfig.UserSessionCfg;
import com.weipu.common.util.Logger;
import com.weipu.common.util.MyTrafficStat;
import com.weipu.common.util.NetWorkUtil;
import com.weipu.common.util.StringUtil;

/**
 * 用户服务提供者实现类
 *
 * @author hjd008
 * @version [版本号, 2012-9-29]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */

public class UserServiceProviderImpl extends BaseServiceProviderImpl implements UserServiceProvider {

    private static Map<String, String> mapUrl;

    private static Map<String, HttpMethod> mapMethod;


    // 初始化方法名和url键和值存放
    static {
        mapUrl = new HashMap<String, String>();

        // -------------------会员模块---------------------
        mapUrl.put(ServerConstant.RequestMethodNameConstant.METHOD_LOGIN,
                Constant.LOGIN_URL);
        mapUrl.put(ServerConstant.RequestMethodNameConstant.METHOD_REGISTER,
                Constant.REGISTER_URL);
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_GETACCOUNTFORUSER,
                Constant.GETACCOUNTFORUSER_URL);
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_UPDATEACCUNTFORUSER,
                Constant.UPDATEACCUNTFORUSER_URL);
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_CHANGEPASSWORD,
                Constant.CHANGEPASSWORD_URL);
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_FORGETPASSWORD,
                Constant.FORGOTPASSWORD_URL);
        mapUrl.put(ServerConstant.RequestMethodNameConstant.METHOD_ALLCOUNTRY,
                Constant.ALLCOUNTRY_URL);
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_CUSOTMERREVIEWLIST,
                Constant.CUSOTMERREVIEWLIST_URL);
        mapUrl.put(ServerConstant.RequestMethodNameConstant.METHOD_CACHES,
                Constant.CACHES_URL);

        // --------------------WishList-----------------------------
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_DELWISHLISTFORUSER,
                Constant.DELWISHLISTFORUSER_URL);
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_GETCOUNTWISHLISTFORUSER,
                Constant.GETCOUNTWISHLISTFORUSER_URL);
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_WISHLISTINFOFORUSER,
                Constant.WISHLISTINFOFORUSER_URL);
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_ADDWISHLISTFORUSER,
                Constant.ADDWISHLISTFORUSER_URL);

        // //收货地址
        mapUrl.put(ServerConstant.RequestMethodNameConstant.METHOD_ADD_ADDRESS,
                Constant.ADD_ADDRESS_URL);
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_DELETE_ADDRESS,
                Constant.DELETE_ADDRESS_URL);
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_UPDATE_ADDRESS,
                Constant.UPDATE_ADDRESS_URL);
        mapUrl.put(ServerConstant.RequestMethodNameConstant.METHOD_GET_ADDRESS,
                Constant.GET_ADDRESS_URL);
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_SET_DEFAULT_ADDRESS,
                Constant.SET_DEFAULT_ADDRESS_URL);
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_GET_DEFAULT_ADDRESS,
                Constant.GET_DEFAULT_ADDRESS_URL);

        //一键过滤
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_GETCUSTOMERFILTER,
                Constant.GETCUSTOMERFILTER_URL);
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_SAVECUSTOMERFILTER,
                Constant.SAVECUSTOMERFILTER_URL);
        mapUrl.put(
                ServerConstant.RequestMethodNameConstant.METHOD_SAVECUSTOMERISFILTER,
                Constant.SAVECUSTOMERISFILTER_URL);
    }

    // 初始化方法名和httpmetod键和值存放
    static {
        mapMethod = new HashMap<String, HttpMethod>();

        // -------------------会员模块---------------------
        mapMethod.put(ServerConstant.RequestMethodNameConstant.METHOD_LOGIN,
                HttpMethod.POST);
        mapMethod.put(ServerConstant.RequestMethodNameConstant.METHOD_REGISTER,
                HttpMethod.POST);
        mapMethod
                .put(ServerConstant.RequestMethodNameConstant.METHOD_GETACCOUNTFORUSER,
                        HttpMethod.GET);
        mapMethod
                .put(ServerConstant.RequestMethodNameConstant.METHOD_UPDATEACCUNTFORUSER,
                        HttpMethod.PUT);
        mapMethod.put(
                ServerConstant.RequestMethodNameConstant.METHOD_CHANGEPASSWORD,
                HttpMethod.PUT);
        mapMethod.put(
                ServerConstant.RequestMethodNameConstant.METHOD_FORGETPASSWORD,
                HttpMethod.GET);
        mapMethod.put(
                ServerConstant.RequestMethodNameConstant.METHOD_ALLCOUNTRY,
                HttpMethod.GET);
        mapMethod
                .put(ServerConstant.RequestMethodNameConstant.METHOD_CUSOTMERREVIEWLIST,
                        HttpMethod.GET);
        mapMethod.put(ServerConstant.RequestMethodNameConstant.METHOD_CACHES,
                HttpMethod.GET);

        // --------------------WishList-----------------------------
        mapMethod
                .put(ServerConstant.RequestMethodNameConstant.METHOD_DELWISHLISTFORUSER,
                        HttpMethod.DELETE);
        mapMethod
                .put(ServerConstant.RequestMethodNameConstant.METHOD_GETCOUNTWISHLISTFORUSER,
                        HttpMethod.GET);
        mapMethod
                .put(ServerConstant.RequestMethodNameConstant.METHOD_WISHLISTINFOFORUSER,
                        HttpMethod.POST);
        mapMethod
                .put(ServerConstant.RequestMethodNameConstant.METHOD_ADDWISHLISTFORUSER,
                        HttpMethod.POST);

        mapMethod.put(
                ServerConstant.RequestMethodNameConstant.METHOD_GET_ADDRESS,
                HttpMethod.POST);
        mapMethod.put(
                ServerConstant.RequestMethodNameConstant.METHOD_DELETE_ADDRESS,
                HttpMethod.POST);
        mapMethod.put(
                ServerConstant.RequestMethodNameConstant.METHOD_UPDATE_ADDRESS,
                HttpMethod.POST);
        mapMethod.put(
                ServerConstant.RequestMethodNameConstant.METHOD_ADD_ADDRESS,
                HttpMethod.POST);
        mapMethod
                .put(ServerConstant.RequestMethodNameConstant.METHOD_SET_DEFAULT_ADDRESS,
                        HttpMethod.POST);
        mapMethod
                .put(ServerConstant.RequestMethodNameConstant.METHOD_GET_DEFAULT_ADDRESS,
                        HttpMethod.POST);
        //一键过滤
        mapMethod
                .put(ServerConstant.RequestMethodNameConstant.METHOD_SAVECUSTOMERFILTER,
                        HttpMethod.POST);
    }


    /**
     * 调用服务端接口put和post的封装方法
     *
     * @param methodName
     * @param map
     * @return String [返回类型说明]
     * @throws throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private JSONObject postOrPutWebInterface(String methodName, Object object)
            throws DxException {
        String result = null;
        JSONObject json = null;
        try {
            String currencyCode = StringUtil.getConfig(ServerConstant.Parameter.PARAMETER_NAME_CURRENCYCODE, "USD");
            OkHttpHelper okHttpHelper = OkHttpHelper.getOkHttpHelperInstance();
            if (HttpMethod.PUT.equals(mapMethod.get(methodName))) {
                result = okHttpHelper.syncPutRequest(mapUrl.get(methodName), object, currencyCode);
            } else {
                result = okHttpHelper.syncPostRequest(mapUrl.get(methodName), object, currencyCode);
            }
            Log.i("Result", mapMethod.get(methodName) + "  " + object.toString());
            // 服务内部错误
            if (null == result) {
                throw new DxException(
                        ServerConstant.ReturnCode.STATUS_INTENAL_ERROR);
            }
            json = returnResult(result);
        } catch (IOException e) {
            Logger.e(e.getMessage(), e);
            Logger.i("AddAddress", "end sssss" + e);
            // 链接超时
            if (e.getCause() instanceof SocketTimeoutException
                    || e.getCause() instanceof ConnectTimeoutException) {
                throw new DxException(
                        ServerConstant.ReturnCode.VALIDATOR_CONNECT_TIMEOUT);
            } else if (e.getCause() instanceof UnknownHostException) {
                throw new DxException(ServerConstant.ReturnCode.CLIENT_NET_UN_AVAILABLE);
            } else {
                if (!NetWorkUtil.isNetworkAvailable(DXApplication.getContext())) {
                    throw new DxException(
                            ServerConstant.ReturnCode.CLIENT_NET_UN_AVAILABLE);
                } else {
                    throw new DxException(
                            ServerConstant.ReturnCode.VALIDATOR_UNKONW_ERROR);
                }
            }
        }
        return json;
    }

    /*
     * 登录
     *
     * @param email
     *
     * @param password
     *
     * @return
     *
     * @see com.weipu.common.facade.UserServiceProvider#login(java.lang.String,
     * java.lang.String)
     */
    @Override
    public UserModel login(String email, String password) throws DxException {
        Messages messages = new Messages();
        messages.setPassword(password);
        messages.setAccount(email);
        JSONObject result = null;
        Logger.i("login", "star");
        result = postOrPutWebInterface(
                ServerConstant.RequestMethodNameConstant.METHOD_LOGIN, messages);
        MyTrafficStat
                .getRxbytes(ServerConstant.RequestMethodNameConstant.METHOD_LOGIN);
        JSONObject jsons = result
                .optJSONObject(ServerConstant.Parameter.PARAMETER_NAME_DATA);
        JSONObject js = jsons
                .optJSONObject(ServerConstant.Parameter.PARAMETER_NAME_CUSTOMER);
        Logger.i("changePassword", "star" + result.toString());
        UserSessionCfg cfg = GlobalConfig.getInstance().getUserSessionCfg();
        cfg.setUsername(js.optString(ServerConstant.User.CONSTANT_USERNAME));
        cfg.setCustomerid(js.optString(ServerConstant.User.CONSTANT_CUSTOMERID));
        cfg.setEmail(js.optString(ServerConstant.User.CONSTANT_EMAIL));
        cfg.setToken(jsons.optString(ServerConstant.User.CONSTANT_TOKEN));
        UserModel userModel = UserModel.json2user(js, jsons.optString(ServerConstant.User.CONSTANT_TOKEN));
        return userModel;
    }

    /*
     * 注册
     *
     * @param email
     *
     * @param userName
     *
     * @param firstName
     *
     * @param lastName
     *
     * @param passWord
     *
     * @return
     *
     * @see
     * com.weipu.common.facade.UserServiceProvider#register(java.lang.String,
     * java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public String register(String email, String userName, String firstName,
                           String lastName, String passWord) throws DxException {
        Messages messages = new Messages();
        messages.setEmail(email);
        messages.setUserName(userName);
        messages.setFirstName(firstName);
        messages.setLastName(lastName);
        messages.setPassword(passWord);
        JSONObject result = null;
        Logger.i("register", "star");
        result = postOrPutWebInterface(
                ServerConstant.RequestMethodNameConstant.METHOD_REGISTER,
                messages);
        Logger.i("register", "end");
        MyTrafficStat
                .getRxbytes(ServerConstant.RequestMethodNameConstant.METHOD_REGISTER);
        JSONObject json = result.optJSONObject(Constant.JSON_DATA);
        String customerid = json.optString(
                ServerConstant.User.CONSTANT_CUSTOMERID).toString();
        return customerid;
    }

    /*
     * 获取用户信息
     *
     * @param customerId
     *
     * @return
     *
     * @see
     * com.weipu.common.facade.UserServiceProvider#getAccountForUser(java.lang
     * .String)
     */
    @Override
    public UserModel getAccountForUser(String customerId, String token) throws DxException {
        JSONObject js = requestHttpGetData(mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_GETACCOUNTFORUSER)
                + customerId + "/User" + "?Token=" + token);
        return UserModel.json2user(js.optJSONObject(Constant.JSON_CUSTOMER), null);
    }

    /*
     * 忘记密码
     *
     * @param email
     *
     * @return
     *
     * @see
     * com.weipu.common.facade.UserServiceProvider#forgetPassword(java.lang.
     * String)
     */
    @Override
    public boolean forgetPassword(String email) throws DxException {
        JSONObject result1 = requestHttpGetData(mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_FORGETPASSWORD)
                + email + "/ForgetPwd");
        return result1.optBoolean("ApplyEmailIsSend", false);
    }

    /*
     * 修改用户信息
     *
     * @param FirstName
     *
     * @param LastName
     *
     * @param Gender
     *
     * @param Birthday
     *
     * @param Education
     *
     * @param MaritalStatus
     *
     * @param CountryCode
     *
     * @param Hobby
     *
     * @param Income
     *
     * @return
     *
     * @see
     * com.weipu.common.facade.UserServiceProvider#updateAccuntForUser(java.
     * lang.String, java.lang.String, short, java.lang.String, short, short,
     * java.lang.String, java.util.List, short)
     */
    @Override
    public String updateAccuntForUser(String FirstName, String LastName,
                                      short Gender, String Birthday, short Education,
                                      short MaritalStatus, String CountryCode, List<Short> Hobby,
                                      short Income, String customerid, String token) throws DxException {
        Messages messages = new Messages();
        messages.setFirstName(FirstName);
        messages.setLastName(LastName);
        messages.setGender(Gender);
        messages.setBirthday(Birthday);
        messages.setEducation(Education);
        messages.setMaritalStatus(MaritalStatus);
        messages.setCountry(CountryCode);
        messages.setHobby(Hobby);
        messages.setIncome(Income);
        messages.setToken(token);
        JSONObject json = requestHttpPutAll(Constant.UPDATEACCUNTFORUSER_URL + customerid + "/Save", null);
        return json.optString(Constant.JSON_STATUS);
    }

    /*
     * 修改密码
     *
     * @param customerId
     *
     * @param oldPwd
     *
     * @param newPwd
     *
     * @return
     *
     * @see com.weipu.common.facade.UserServiceProvider#changePassword(long,
     * java.lang.String, java.lang.String)
     */
    @Override
    public int changePassword(String customerId, String oldPwd, String newPwd, String token)
            throws DxException {
        Messages messages = new Messages();
        messages.setCustomerId(customerId);
        messages.setCustomerId(Long.parseLong(customerId));
        messages.setOldPwd(oldPwd);
        messages.setNewPwd(newPwd);
        messages.setToken(token);
        Logger.i("changePassword", "star " + token + " " + customerId);
        JSONObject result = postOrPutWebInterface(
                ServerConstant.RequestMethodNameConstant.METHOD_CHANGEPASSWORD,
                messages);
        Logger.i("changePassword", "end");
        Logger.d("---", "-" + result.toString());
        return result.optInt(Constant.JSON_STATUS);
    }

    /*
     * 获取国家码
     *
     * @return
     *
     * @see com.weipu.common.facade.UserServiceProvider#allCountry()
     */
    @Override
    public CountryModel allCountry() throws DxException {
        Map<String, String> jsonMap = new HashMap<String, String>();
        JSONObject js;
        JSONObject jsons = requestHttpGetData(mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_ALLCOUNTRY));
        JSONArray jsonss = jsons.optJSONArray("AllCountry");
        String key;
        try {
            js = jsonss.getJSONObject(0);
            Iterator iterator = js.keys();
            while (iterator.hasNext()) {
                key = iterator.next().toString();
                jsonMap.put(key, (String) js.get(key));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Logger.d("---", "--" + jsonMap.toString());
        Map<String, String> countryMap = jsonMap;
        CountryModel countryModel = new CountryModel();
        countryModel.setAllCountry(countryMap);
        return countryModel;
    }

    /*
     * 获取我的商品评论列表
     *
     * @param SKU
     *
     * @param CICID
     *
     * @param userName
     *
     * @param pageIndex
     *
     * @param pageSize
     *
     * @return
     *
     * @see com.weipu.common.facade.UserServiceProvider#cusotmerReviewList(int,
     * long, java.lang.String, int, int)
     */
    @Override
    public ArrayList<ReviewlistModel> cusotmerReviewList(int SKU, long CICID, String userName,
                                                         int pageIndex, int pageSize, String token) throws DxException {
        ArrayList<ReviewlistModel> reviewlistModels = new ArrayList<ReviewlistModel>();
        JSONObject json_data = requestHttpGetData(mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_CUSOTMERREVIEWLIST)
                + "?SKU="
                + SKU
                + "&Token="
                + token
                + "&CICID="
                + CICID
                + "&userName="
                + userName
                + "&pageIndex="
                + pageIndex + "&pageSize=" + pageSize);
        JSONArray json_resultset = json_data
                .optJSONArray(Constant.JSON_RESULTSET);
        reviewlistModels = ReviewlistModel.jsonarray2product(json_resultset);

        return null;
    }

    /*
     * 添加商品到心愿单
     *
     * @param UserName
     *
     * @param sku
     *
     * @param Source
     *
     * @return
     *
     * @see
     * com.weipu.common.facade.UserServiceProvider#addWishlistForUser(java.lang
     * .String, int, java.lang.String)
     */
    @Override
    public String addWishlistForUser(int sku, String Source, long customerID, String token)
            throws DxException {
        Messages messages = new Messages();
        messages.setCustomerId(customerID);
        messages.setSku(sku);
        messages.setSource(Source);
        messages.setToken(token);
        Logger.i("addWishlistForUser", "star");
        JSONObject result = postOrPutWebInterface(
                ServerConstant.RequestMethodNameConstant.METHOD_ADDWISHLISTFORUSER,
                messages);
        Logger.i("addWishlistForUser", "end");
        Logger.d("-", "-" + result.toString());
        return result.optString(Constant.JSON_STATUS);
    }

    /*
     * 删除心愿单
     *
     * @param username
     *
     * @param sku
     *
     * @return
     *
     * @see
     * com.weipu.common.facade.UserServiceProvider#delWishlistForUser(java.lang
     * .String, int)
     */
    @Override
    public String delWishlistForUser(String username, int sku, String token)
            throws DxException {
        String result = null;
        JSONObject json = null;
        try {
            OkHttpHelper helper = OkHttpHelper.getOkHttpHelperInstance();
            result = helper.syncDeleteRequest(mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_DELWISHLISTFORUSER)
                    + "?username=" + username + "&sku=" + sku
                    + "&Token=" + token, null, null);
            Logger.i("delWishlistForUser", "end");
        } catch (IOException e) {
            Logger.e(e.getMessage(), e);
            // 链接超时
            if (e.getCause() instanceof SocketTimeoutException
                    || e.getCause() instanceof ConnectTimeoutException) {
                throw new DxException(
                        ServerConstant.ReturnCode.VALIDATOR_CONNECT_TIMEOUT);
            } else if (e.getCause() instanceof UnknownHostException) {
                throw new DxException(ServerConstant.ReturnCode.CLIENT_NET_UN_AVAILABLE);
            } else {
                if (!NetWorkUtil.isNetworkAvailable(DXApplication.getContext())) {
                    throw new DxException(
                            ServerConstant.ReturnCode.CLIENT_NET_UN_AVAILABLE);
                } else {
                    throw new DxException(
                            ServerConstant.ReturnCode.VALIDATOR_UNKONW_ERROR);
                }
            }
        }
        json = returnResult(result);
        Logger.d("--", "--" + json.toString());

        return json.optString(Constant.JSON_STATUS);
    }

    /*
     * 获取用户收藏夹总数
     *
     * @param username
     *
     * @return
     *
     * @see
     * com.weipu.common.facade.UserServiceProvider#getCountWishlistForUser(java
     * .lang.String)
     */
    @Override
    public String getCountWishlistForUser(String username, String token) throws DxException {
        String result = null;
        try {
            Logger.i("getCountWishlistForUser", "star");
            OkHttpHelper helper = OkHttpHelper.getOkHttpHelperInstance();
            result = helper.syncGetRequest(mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_GETCOUNTWISHLISTFORUSER)
                    + "?username=" + username
                    + "&Token="
                    + token, null);
            Logger.i("getCountWishlistForUser", "end");
        } catch (IOException e) {
            Logger.e(e.getMessage(), e);
            // 链接超时
            if (e.getCause() instanceof SocketTimeoutException
                    || e.getCause() instanceof ConnectTimeoutException) {
                throw new DxException(
                        ServerConstant.ReturnCode.VALIDATOR_CONNECT_TIMEOUT);
            } else if (e.getCause() instanceof UnknownHostException) {
                throw new DxException(ServerConstant.ReturnCode.CLIENT_NET_UN_AVAILABLE);
            } else {
                if (!NetWorkUtil.isNetworkAvailable(DXApplication.getContext())) {
                    throw new DxException(
                            ServerConstant.ReturnCode.CLIENT_NET_UN_AVAILABLE);
                } else {
                    throw new DxException(
                            ServerConstant.ReturnCode.VALIDATOR_UNKONW_ERROR);
                }
            }
        }
        JSONObject json = returnResult(result);
        Logger.d("---", "--" + json.toString());
        JSONObject js = json.optJSONObject(Constant.JSON_DATA);

        return js.optString(ServerConstant.Wish.CONSTANT_WISHLISTCOUNT);
    }

    /*
     * 获取心愿单列表
     *
     * @param UserName
     *
     * @param WishSort
     *
     * @param PageIndex
     *
     * @param PageSize
     *
     * @return
     *
     * @see
     * com.weipu.common.facade.ProductServiceProvider#wishListInfoForUser(java
     * .lang.String, java.lang.String, int, int)
     */
    @Override
    public ArrayList<WishModel> wishListInfoForUser(String UserName,
                                                    String WishSort, int PageIndex, int PageSize, long customerId, String token) throws DxException {
        Messages messages = new Messages();
        messages.setUserName(UserName);
        messages.setWishSort(WishSort);
        messages.setPageIndex(PageIndex);
        messages.setPageSize(PageSize);
        //需设置为Long类型
        messages.setCustomerId(customerId);
        messages.setToken(token);
        JSONObject result;
        ArrayList<WishModel> list = null;
        Logger.i("wishListInfoForUser", "star");
        result = postOrPutWebInterface(
                ServerConstant.RequestMethodNameConstant.METHOD_WISHLISTINFOFORUSER,
                messages);
        Logger.i("wishListInfoForUser", "end");
        Logger.d("----", "---" + result.toString());
        JSONObject data = result.optJSONObject(Constant.JSON_DATA);
        JSONArray json = data.optJSONArray(Constant.JSON_WISHLIST);
        if (null != json) {
            list = new ArrayList<WishModel>();
            for (int i = 0; i < json.length(); i++) {
                WishModel wishModel = WishModel.jsonArray2Wish(json.optJSONObject(i));
                wishModel.setCurrencyCode(data.optString(ServerConstant.Parameter.PARAMETER_NAME_CURRENCYCODE));
                wishModel.setCurrencyCodeSymbol(data.optString(ServerConstant.Parameter.PARAMETER_NAME_CURRENCYCODE_SYMBOL));
                list.add(wishModel);
            }
        }


        return list;
    }

    /*
     * 用户缓存配置
     *
     * @return
     *
     * @throws DxException
     *
     * @see com.weipu.common.facade.UserServiceProvider#caches()
     */
    @Override
    public CacheConfigModel getCacheConfig() throws DxException {
        String result = null;
        JSONObject json;
        CacheConfigModel cacheConfigModel = new CacheConfigModel();
        try {
            Logger.i("Caches", "star");
            OkHttpHelper helper = OkHttpHelper.getOkHttpHelperInstance();
            result = helper.syncGetRequest(mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_CACHES), null);
            Logger.i("Caches", "end");
        } catch (IOException e) {
            Logger.e(e.getMessage(), e);
            // 链接超时
            if (e.getCause() instanceof SocketTimeoutException
                    || e.getCause() instanceof ConnectTimeoutException) {
                throw new DxException(
                        ServerConstant.ReturnCode.VALIDATOR_CONNECT_TIMEOUT);
            } else if (e.getCause() instanceof UnknownHostException) {
                throw new DxException(ServerConstant.ReturnCode.CLIENT_NET_UN_AVAILABLE);
            } else {
                if (!NetWorkUtil.isNetworkAvailable(DXApplication.getContext())) {
                    throw new DxException(
                            ServerConstant.ReturnCode.CLIENT_NET_UN_AVAILABLE);
                } else {
                    throw new DxException(
                            ServerConstant.ReturnCode.VALIDATOR_UNKONW_ERROR);
                }
            }
        }
        Logger.d("---", "-" + result);
        json = returnResult(result);
        JSONObject data = json.optJSONObject(Constant.JSON_DATA);
        JSONArray model = data.optJSONArray("Model");
        try {
            if (model != null) {
                cacheConfigModel = CacheConfigModel.json2Cache(model
                        .getJSONObject(0));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return cacheConfigModel;
    }

    @Override
    public String AddAddress(AddressModel addressModel) throws DxException {
        Logger.i("AddAddress", "star");
        JSONObject result = postOrPutWebInterface(
                ServerConstant.RequestMethodNameConstant.METHOD_ADD_ADDRESS,
                addressModel);
        JSONObject data = result.optJSONObject(Constant.JSON_DATA);
        String addressId = "";
        if (null != data) {
            addressId = data
                    .optString(ServerConstant.Address.CONSTANT_ADDRESSID) + "";
        }
        Logger.i("AddAddress", "end " + result);
        return addressId;
    }

    @Override
    public String DeleteAddress(long addressId, long customerId, String token) throws DxException {
        Parmars messages = new Parmars();
        messages.setAddressID(addressId);
        messages.setCustomerID(customerId);
        messages.setToken(token);
        JSONObject result = postOrPutWebInterface(
                ServerConstant.RequestMethodNameConstant.METHOD_DELETE_ADDRESS,
                messages);
        Logger.i("DeleteAddress", "end " + result);
        return result.optString(Constant.JSON_STATUS);
    }

    @Override
    public String ChooseDefaultAddress(long addressId, long customerId, String token) throws DxException {
        Parmars messages = new Parmars();
        messages.setAddressID(addressId);
        messages.setCustomerID(customerId);
        messages.setToken(token);
        JSONObject result = postOrPutWebInterface(
                ServerConstant.RequestMethodNameConstant.METHOD_SET_DEFAULT_ADDRESS,
                messages);
        Logger.i("DeleteAddress", "end " + result);
        return result.optString(Constant.JSON_STATUS);
    }

    @Override
    public List<AddressModel> GetAddresses(long CustomerID, int AddressType,
                                           int PageIndex, int PageSize, String token) throws DxException {
        Parmars messages = new Parmars();
        messages.setCustomerID(CustomerID);
        messages.setAddressType(AddressType);
        messages.setPageIndex(PageIndex);
        messages.setPageSize(PageSize);
        messages.setToken(token);
        JSONObject result = postOrPutWebInterface(
                ServerConstant.RequestMethodNameConstant.METHOD_GET_ADDRESS,
                messages);
        Logger.i("GetAddresses", "end " + result);
        JSONObject data = result.optJSONObject(Constant.JSON_DATA);
        JSONArray json = data
                .optJSONArray(ServerConstant.Address.CONSTANT_ADDRESSES);
        // TODO Auto-generated method stub
        return AddressModel.jsonArray2Address(json);
    }

    @Override
    public String UpdateAddress(AddressModel addressModel) throws DxException {
        Logger.i("UpdateAddress", "star");
        JSONObject result = postOrPutWebInterface(
                ServerConstant.RequestMethodNameConstant.METHOD_UPDATE_ADDRESS,
                addressModel);
        Logger.i("UpdateAddress", "end " + result);
        return result.optString(Constant.JSON_STATUS);
    }

    @Override
    public AddressModel GetDefaultAddress(long customerId, int addressType, String token)
            throws DxException {
        Parmars messages = new Parmars();
        messages.setCustomerID(customerId);
        messages.setAddressType(addressType);
        messages.setToken(token);
        Logger.i("GetDefaultAddress", messages.toString());
        JSONObject result = postOrPutWebInterface(
                ServerConstant.RequestMethodNameConstant.METHOD_GET_DEFAULT_ADDRESS,
                messages);
        Logger.i("GetDefaultAddress", "end " + result);
        if (null != result) {
            JSONObject data = result.optJSONObject(Constant.JSON_DATA);
            JSONObject json = data
                    .optJSONObject(ServerConstant.Address.CONSTANT_ADDRESS);
            if (null != json) {
                return AddressModel.jsonAddress(json);
            } else {
                return null;
            }
            // TODO Auto-generated method stub

        } else {
            return null;
        }

    }

    @Override
    public OneKeyFilterResponseModel getCustomerFilter(String token,
                                                       long customerID) throws DxException {
        String result = null;
        try {
            OkHttpHelper helper = OkHttpHelper.getOkHttpHelperInstance();
            result = helper.syncGetRequest(mapUrl.get(ServerConstant.RequestMethodNameConstant.METHOD_GETCUSTOMERFILTER)
                    + "?Token=" + token + "&CustomerID=" + customerID, null);
        } catch (IOException e) {
            Logger.e(e.getMessage(), e);
            // 链接超时
            if (e.getCause() instanceof SocketTimeoutException
                    || e.getCause() instanceof ConnectTimeoutException) {
                throw new DxException(
                        ServerConstant.ReturnCode.VALIDATOR_CONNECT_TIMEOUT);
            } else if (e.getCause() instanceof UnknownHostException) {
                throw new DxException(ServerConstant.ReturnCode.CLIENT_NET_UN_AVAILABLE);
            } else {
                if (!NetWorkUtil.isNetworkAvailable(DXApplication.getContext())) {
                    throw new DxException(
                            ServerConstant.ReturnCode.CLIENT_NET_UN_AVAILABLE);
                } else {
                    throw new DxException(
                            ServerConstant.ReturnCode.VALIDATOR_UNKONW_ERROR);
                }
            }
        }
        if (null == result) {
            return null;
        }
        JSONObject jsonObject = returnResult(result);
        if (null == jsonObject) {
            return null;
        }
        JSONObject optJSONObject = jsonObject.optJSONObject("Data");
        return OneKeyFilterResponseModel.json2Model(optJSONObject);
    }

    @Override
    public OneKeyFilterResponseModel saveCustomerFilter(String token,
                                                        long customerID, boolean isFilter,
                                                        String blackCategoryIds) throws DxException {
        Messages messages = new Messages();
        messages.setToken(token);
        messages.setCustomerId(customerID);
        messages.setIsFilter(isFilter);
        messages.setBlackCategoryIds(blackCategoryIds);
        JSONObject data = postOrPutWebInterface(ServerConstant.RequestMethodNameConstant.METHOD_SAVECUSTOMERFILTER, messages);
        return OneKeyFilterResponseModel.json2Model(data);
    }

    @Override
    public void SaveCustomerIsFilter(String token, long customerID,
                                     boolean isFilter) throws DxException {
        Messages messages = new Messages();
        messages.setToken(token);
        messages.setCustomerId(customerID);
        messages.setIsFilter(isFilter);
        postOrPutWebInterface(ServerConstant.RequestMethodNameConstant.METHOD_SAVECUSTOMERISFILTER, messages);
    }

}
